package com.heiyu.api.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.heiyu.api.request.CoinListRequets;
import com.heiyu.api.request.CoinPageRequets;
import com.heiyu.api.response.CoinListResponse;
import com.heiyu.api.response.CoinPageResponse;
import com.heiyu.api.response.model.CoinListInfo;
import com.heiyu.api.response.model.CoinListModel;
import com.heiyu.api.response.model.CoinPageModel;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.CoinBaseInfoMapper;
import com.heiyu.dao.GlobalShowInfoMapper;
import com.heiyu.dao.UserFollowMapper;
import com.heiyu.entity.CoinBaseInfo;
import com.heiyu.entity.GlobalShowInfo;
import com.heiyu.entity.UserFollow;
import com.heiyu.entity.UserInfo;
import com.heiyu.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by mountain on 18/6/11.
 */
@Api
@RestController
@RequestMapping("api/v1/coin")
public class CoinInfoController extends ApiBaseController{

    @Autowired
    private CoinBaseInfoMapper coinBaseInfoMapper;
    @Autowired
    private GlobalShowInfoMapper globalShowInfoMapper;
    @Autowired
    private UserFollowMapper userFollowMapper;

    @ApiOperation(value = "list",notes = "币种大全列表")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public CoinListResponse list(@ModelAttribute CoinListRequets request){
           UserInfo userInfo = getUser();
            CoinListResponse response = new CoinListResponse();
            CoinListModel model = new CoinListModel();
            Map<String,Object> param = PageUtils.newParam(1,10);
            param.put("tradePriceDesc","desc");
            List<GlobalShowInfo> hoteCoinList = globalShowInfoMapper.selectByOrderDesc(param);
            List<UserFollow> follows = null;
            if(userInfo != null){
                UserFollow follow = new UserFollow();
                follow.setUserName(getUser().getTelphone());
                follow.setType("1");
                follows = this.userFollowMapper.selectList(follow);
                model.setCollectMap(Maps.newHashMap());
                for(UserFollow f:follows){
                    model.getCollectMap().put(f.getFCoin(),f.getId());
                }
            }
            model.setHotCoinList(formatterCoinInfo(hoteCoinList,follows));
            response.setData(model);
            response.success();
            return response;
       /*UserInfo userInfo = getUser();
        CoinListResponse response = new CoinListResponse();
        CoinListModel model = new CoinListModel();
        Map<String,Object> param = PageUtils.newParam(1,10);
        param.put("tradePriceDesc","desc");
        List<GlobalShowInfo> hoteCoinList = globalShowInfoMapper.selectByOrderDesc(param);
        List<CoinBaseInfo> list  = coinBaseInfoMapper.selectList(new CoinBaseInfo());
        model.setCoinList(Maps.newHashMap());
        for(int i=0;i<26;i++){
            char key = (char) (i+65);
            model.getCoinList().put(""+key,Lists.newArrayList());
        }
        model.getCoinList().put("#",Lists.newArrayList());
        CoinListInfo listInfo;
        List<UserFollow> follows = null;
        if(userInfo != null){
            UserFollow follow = new UserFollow();
            follow.setUserName(getUser().getTelphone());
            follows = this.userFollowMapper.selectList(follow);
        }

        for(CoinBaseInfo info : list){
            listInfo = new CoinListInfo();
            listInfo.setName(info.getSymbol());
            listInfo.setLogoUrl(info.getLogo());
            listInfo.setFullName(info.getUsName());
            listInfo.setTargetId(info.getTargetId());
            listInfo.setCnName(info.getCnName());
            if(follows != null){
                for(int i=0;i<follows.size();i++){
                    if(follows.get(i).getFCoin().equals(info.getTargetId())){
                        listInfo.setCollectId(follows.get(i).getId());
                        follows.remove(i);
                        break;
                    }
                }
            }
            try{

                if(listInfo.getName() != null && model.getCoinList().containsKey(listInfo.getName().substring(0,1))){
                    model.getCoinList().get(listInfo.getName().substring(0,1)).add(listInfo);
                }else{
                    model.getCoinList().get("#").add(listInfo);
                }
            }catch (Exception e){
                continue;
            }

        }
        model.setHotCoinList(formatterCoinInfo(hoteCoinList,follows));
        response.setData(model);
        response.success();
        return response;*/
    }


    @ApiOperation(value = "coin/page/list")
    @RequestMapping(value = "coin/page/list",method = RequestMethod.GET)
    public CoinPageResponse coinPageList(@ModelAttribute CoinPageRequets requets){
        CoinPageResponse response = new CoinPageResponse();
        CoinPageModel model = new CoinPageModel();
        model.setList(Lists.newArrayList());
        if(requets.getType()==null){
            requets.setType("1");
        }
        if(requets.getPageIndex()==null){
            requets.setPageIndex(1);
        }
        if(requets.getPageSize() == null){
            requets.setPageSize(20);
        }
        CoinBaseInfo coinBaseInfo;
        CoinListInfo coinListInfo;
        if(requets.getType().equals("1")){
            Map<String,Object> param = PageUtils.newParam(1,10);
            param.put("tradePriceDesc","desc");
            List<GlobalShowInfo> hoteCoinList = globalShowInfoMapper.selectByOrderDesc(param);
            for(GlobalShowInfo info:hoteCoinList){
                coinBaseInfo = coinBaseInfoUtils.get(info.getTargetId());
                coinListInfo = new CoinListInfo();
                coinListInfo.setTargetId(coinBaseInfo.getTargetId());
                coinListInfo.setLogoUrl(coinBaseInfo.getLogo());
                coinListInfo.setName(coinBaseInfo.getSymbol());
                coinListInfo.setFullName(coinBaseInfo.getUsName());
                coinListInfo.setCnName(coinBaseInfo.getCnName());
                model.getList().add(coinListInfo);
            }
        }else{
            coinBaseInfo  =  new CoinBaseInfo();
            List<CoinBaseInfo> list = this.coinBaseInfoMapper.selectList(coinBaseInfo);
            if(list.size()>=requets.getPageIndex()*requets.getPageSize()){
                for(int i=(requets.getPageIndex()-1)*requets.getPageSize();i<requets.getPageIndex()*requets.getPageSize();i++){
                    coinListInfo = new CoinListInfo();
                    coinListInfo.setTargetId(list.get(i).getUsName());
                    coinListInfo.setLogoUrl(list.get(i).getLogo());
                    coinListInfo.setName(list.get(i).getSymbol());
                    coinListInfo.setFullName(list.get(i).getUsName());
                    coinListInfo.setCnName(list.get(i).getCnName());
                    model.getList().add(coinListInfo);
                }
            }
        }
        response.setData(model);
        response.success();
        return response;
    }
    private List<CoinListInfo> formatterCoinInfo(List<GlobalShowInfo> list,List<UserFollow> follows){
        List<CoinListInfo> result = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(list)){
            CoinListInfo coinListInfo;
            CoinBaseInfo coinBaseInfo;
            for(GlobalShowInfo info:list){
                try{
                    coinBaseInfo = new CoinBaseInfo();
                    coinBaseInfo.setTargetId(info.getTargetId());
                    coinBaseInfo = this.coinBaseInfoMapper.selectList(coinBaseInfo).get(0);
                    coinListInfo = new CoinListInfo();
                    coinListInfo.setLogoUrl(coinBaseInfo.getLogo());
                    coinListInfo.setFullName(info.getUsName());
                    coinListInfo.setName(info.getSymbol());
                    coinListInfo.setTargetId(info.getTargetId());
                    coinListInfo.setCnName(info.getCnName());
                    if(follows != null){
                        for(int i=0;i<follows.size();i++){
                            if(follows.get(i).getFCoin().equals(info.getTargetId())){
                                coinListInfo.setCollectId(follows.get(i).getId());
                                follows.remove(i);
                                break;
                            }
                        }
                    }
                    result.add(coinListInfo);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
